package org.example.bishe1.service.impl;

import org.example.bishe1.mapper.LoginMapper;
import org.example.bishe1.pojo.user.LoginInfo;
import org.example.bishe1.pojo.user.User;
import org.example.bishe1.pojo.util.JwtUtils;
import org.example.bishe1.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public LoginInfo login(User user) {
        User loginUser = loginMapper.login(user);
        if (loginUser != null){
            //生成jwt令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", loginUser.getId());
            dataMap.put("username", loginUser.getUsername());
            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(loginUser.getId(), loginUser.getUsername(), loginUser.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
}
